package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UISkillDetail extends UIAbstractWindow
{
	//
	private final int STATE_DETAIL = 1;
	private final int STATE_COMPARE = 2;
	private final String TITLE = "技能详情";
	//
	private final int H_PRE = UITools.H_PRE;
	private final int W_RECT = UITools.W_RECT - UIManager.IMAGE_SCROLL_RULING.getWidth();
	private final int W_CONTENT = W_RECT - (UITools.IW_RECT_CONTENT << 1);
	private int NUM_MAX_ROWS;// 当前页显示的条数
	private int m_h_content;
	//
	private final int COLOR_ATT = UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
	private final int COLOR_STR = Tools.GREEN;
	//
	private final String ATT_PRESKILL_DESCRIBE = "前置技能: ";
	private final String ATT_SKILLNAME = "技能名称: ";
	private final String ATT_NEEDLEVEL = "需要等级: ";
	private final String ATT_MAXLEVEL = "总等级: ";
	private final String ATT_CONSUME_CAREER = "消耗: ";
	private final String ATT_RANGLE = "攻击范围: ";
	private final String ATT_TIME_COSTING = "吟唱时间: ";
	private final String ATT_TIME_COOLING = "冷却时间: ";
	private final String ATT_RESULT = "技能效果: ";
	//
	private String[][] m_preSkill_describes;// 前置技能描述
	private String[] m_names;
	private String[] m_level_needs;// 学习技能需要的最小级别
	private String[] m_level_maxs;//
	private String[] m_consume_careers;// 职业消耗
	private String[] m_ranges;
	private String[] m_time_castings;// 吟唱时间
	private String[] m_time_coolings;// 冷却时间
	private String[][] m_results;// 技能效果
	//
	private int m_y_top;
	private int m_h_sum;

	public UISkillDetail(int id)
	{
		super(id);
		//
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		NUM_MAX_ROWS = (UITools.H_RECT - (UITools.IH_RECT_CONTENT << 1)) / H_PRE;
		m_h_content = NUM_MAX_ROWS * H_PRE + (UITools.IH_RECT_CONTENT << 1);
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP) ) {
			if (m_h_sum <= m_h_content) {
				return;
			}
			if (m_y_top >= 0) {
				return;
			}
			m_y_top += H_PRE;
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_h_sum <= m_h_content) {
				return;
			}
			if (m_y_top <= m_h_content - m_h_sum) {
				return;
			}
			m_y_top -= H_PRE;
		}
	}

	public static final void recvMessage(DataInputStream dis) throws IOException
	{
		short state = dis.readShort();
		String info = dis.readUTF();
		if (!info.equals("")) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
		}
		if (state == 0) {
			return;
		}
		UISkillDetail detail = (UISkillDetail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_SKILLDETAIL);
		int num_skillDescribe = dis.readInt();
		for (int i = 0; i < num_skillDescribe; i++) {
			// 前置技能描述
			if (detail.m_preSkill_describes == null) {
				detail.m_preSkill_describes = new String[num_skillDescribe][];
			}
			String temp = dis.readUTF();
			if (!temp.equals("")) {
				detail.m_preSkill_describes[i] = Tools.getStrArray(temp,AbstractGameScreen.s_font, detail.W_CONTENT);
			}
			// 技能名字
			if (detail.m_names == null) {
				detail.m_names = new String[num_skillDescribe];
			}
			detail.m_names[i] =  dis.readUTF();
			// 需要等级
			if (detail.m_level_needs == null) {
				detail.m_level_needs = new String[num_skillDescribe];
			}
			detail.m_level_needs[i] =  String.valueOf(dis.readByte());
			// 总等级
			if (detail.m_level_maxs == null) {
				detail.m_level_maxs = new String[num_skillDescribe];
			}
			detail.m_level_maxs[i] = String.valueOf(dis.readByte());
			// 职业消耗
			if (detail.m_consume_careers == null) {
				detail.m_consume_careers = new String[num_skillDescribe];
			}
			int career_consume = dis.readInt();
			if (career_consume < 0) {
				detail.m_consume_careers[i] = null;
			}
			else {
				detail.m_consume_careers[i] =  String.valueOf(Math.abs(career_consume));
			}
			// 攻击范围（射程）
			if (detail.m_ranges == null) {
				detail.m_ranges = new String[num_skillDescribe];
			}
			int ranges = dis.readInt();
			if (ranges != 0) {
				detail.m_ranges[i] =  String.valueOf(ranges);
			}
			// 吟唱时间
			if (detail.m_time_castings == null) {
				detail.m_time_castings = new String[num_skillDescribe];
			}
			int time = dis.readInt();
			if (time == 0) {
				detail.m_time_castings[i] =  "瞬发";
			}
			else {
				detail.m_time_castings[i] = time + "秒";
			}
			// 冷却时间
			if (detail.m_time_coolings == null) {
				detail.m_time_coolings = new String[num_skillDescribe];
			}
			int coolTime = dis.readInt();
			if (coolTime != 0) {
				detail.m_time_coolings[i] =   coolTime + "秒";
			}
			// 技能效果
			if (detail.m_results == null) {
				detail.m_results = new String[num_skillDescribe][];
			}
			detail.m_results[i] = Tools.getStrArray( dis.readUTF(), AbstractGameScreen.s_font,detail.W_CONTENT);
		}
		UIManager.getInstance().addWindow(detail);
		if (num_skillDescribe == 1) {
			detail.m_state = detail.STATE_DETAIL;
		}
		else {
			detail.m_state = detail.STATE_COMPARE;
		}
	}

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE, false, true);
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT;
		UITools.paintContentRect(g, tempX, tempY, W_RECT, m_h_content);
		tempX += UITools.IW_RECT_CONTENT ;
		tempY += UITools.IH_RECT_CONTENT ;
		g.setClip(tempX, tempY, W_RECT, m_h_content);
		int contentY = tempY + m_y_top;
		switch (m_state) {
			case STATE_DETAIL: {
				int endY = paintContents(g, tempX, contentY, 0);
				m_h_sum = endY - contentY;
			}
				break;
			case STATE_COMPARE:
			default: {
				int endY = paintContents(g, tempX, contentY, 0);
				endY += H_PRE;
				endY = paintContents(g, tempX, endY, 1);
				m_h_sum = endY - contentY;
			}
				break;
		}
		paintDrag(g, m_x + UITools.X_DRAG, tempY);
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private int paintContents(Graphics g, int ix, int mY, int index)
	{
		// 绘制标题
		int mX = ix + UITools.IW_RECT_CONTENT;
		int tempY = mY + UITools.IH_RECT_CONTENT;
		tempY += paintPreSkillDescribe(g, mX, tempY, index);
		tempY += paintName(g, mX, tempY, index);
		tempY += paintNeedLevel(g, mX, tempY, index);
		tempY += paintMaxLevel(g, mX, tempY, index);
		tempY += paintCareerConsume(g, mX, tempY, index);
		tempY += paintRangle(g, mX, tempY, index);
		tempY += paintTimeCasting(g, mX, tempY, index);
		tempY += paintTimeCool(g, mX, tempY, index);
		tempY += paintResult(g, mX, tempY, index);
		return tempY;
	}

	private int paintPreSkillDescribe(Graphics g, int x, int y, int index)
	{
		if (m_preSkill_describes == null || m_preSkill_describes[index] == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_PRESKILL_DESCRIBE, x, y, 0);
		//
		int tempY = y + H_PRE;
		g.setColor(COLOR_STR);
		int length = m_preSkill_describes[index].length;
		for (int i = 0; i < length; i++) {
			if (m_preSkill_describes[index][i] == null) {
				continue ;
			}
			g.drawString(m_preSkill_describes[index][i], x, tempY, 0);
			tempY += H_PRE;
		}
		return H_PRE * (length+1);
	}

	private int paintName(Graphics g, int x, int y, int index)
	{
		if (m_names == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_SKILLNAME, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_names[index], x + AbstractGameScreen.s_font.stringWidth(ATT_SKILLNAME) + 1, y, 0);
		return H_PRE;
	}

	private int paintNeedLevel(Graphics g, int x, int y, int index)
	{
		if (m_level_needs == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_NEEDLEVEL, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_level_needs[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_NEEDLEVEL) + 1, y, 0);
		return H_PRE;
	}

	private int paintMaxLevel(Graphics g, int x, int y, int index)
	{
		if (m_level_maxs == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_MAXLEVEL, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_level_maxs[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_MAXLEVEL) + 1, y, 0);
		return H_PRE;
	}

	private int paintCareerConsume(Graphics g, int x, int y, int index)
	{
		if (m_consume_careers == null) {
			return 0;
		}
		if (m_consume_careers[index] == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_CONSUME_CAREER, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_consume_careers[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_CONSUME_CAREER) + 1, y, 0);
		return H_PRE;
	}

	private int paintRangle(Graphics g, int x, int y, int index)
	{
		if (m_ranges == null) {
			return 0;
		}
		if (m_ranges[index] == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_RANGLE, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_ranges[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_RANGLE) + 1, y, 0);
		return H_PRE;
	}

	private int paintTimeCasting(Graphics g, int x, int y, int index)
	{
		if (m_time_castings == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_TIME_COSTING, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_time_castings[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_TIME_COSTING) + 1, y, 0);
		return H_PRE;
	}

	private int paintTimeCool(Graphics g, int x, int y, int index)
	{
		if (m_time_coolings == null) {
			return 0;
		}
		if (m_time_coolings[index] == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_TIME_COOLING, x, y, 0);
		g.setColor(COLOR_STR);
		g.drawString(m_time_coolings[index], x+ AbstractGameScreen.s_font.stringWidth(ATT_TIME_COOLING) + 1, y, 0);
		return H_PRE;
	}

	private int paintResult(Graphics g, int x, int y, int index)
	{
		if (m_results == null) {
			return 0;
		}
		g.setColor(COLOR_ATT);
		g.drawString(ATT_RESULT, x, y, 0);
		int tempY = y + H_PRE;
		//
		g.setColor(COLOR_STR);
		int length = m_results[index].length;
		for (int i = 0; i < length; i++) {
			g.drawString(m_results[index][i], x, tempY, 0);
			tempY += H_PRE;
		}
		return H_PRE * (length+1);
	}

	private void paintDrag(Graphics g, int x, int y)
	{
		int len = 0;
		if (m_y_top == 0) {
			len = 0;
		}
		else if (m_y_top <= (m_h_content - m_h_sum)) {
			len = 100;
		}
		else {
			len = (m_y_top * 100) / (m_h_content - m_h_sum);
		}
		UITools.paintScrollBar(g, x, y, m_h_content, len);
	}

	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		
		initTouchRect(new int[][]{
				{446,38,21,37},
				{446,238,21,37},
		});
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		switch(touchid) {
		case 0:
			GameTouchManager.keyPressedDir(1);
			break;
		case 1:
			GameTouchManager.keyPressedDir(3);
			break;
		}
	}
	
	

}
